﻿using MySqlX.XDevAPI.Common;
using NPOI.SS.Formula.Functions;
using NPOI.SS.Formula.PTG;
using Org.BouncyCastle.Asn1.X509;
using RestSharp;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Net.Http;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
using static AutoTestCommunication_Service.Public;

namespace AutoTestCommunication_Service.Class
{
    internal class jiandaoyun
    {
        public static bool GetInformation(string SN, out string PN, out string PRTI, out string RLP, out string PCBSN, out string bengyuanSN,out string PowerPN)
        {
            try
            {
                PN = ""; PRTI = ""; RLP = ""; PCBSN = ""; bengyuanSN = ""; PowerPN = "";
                var client = new RestClient("https://api.jiandaoyun.com/api/v5/app/entry/data/list");
                var request = new RestRequest(Method.POST);
                request.AddHeader("cache-control", "no-cache");
                request.AddHeader("Connection", "keep-alive");
                request.AddHeader("content-length", "371");
                request.AddHeader("accept-encoding", "gzip, deflate");
                request.AddHeader("Host", "api.jiandaoyun.com");
                request.AddHeader("Postman-Token", "eeedcbce-54f2-4c09-b7ef-466b54135b11,5bf9541c-c9f8-4cf2-bc65-cd6d5da2bde8");
                request.AddHeader("Cache-Control", "no-cache");
                request.AddHeader("Accept", "*/*");
                request.AddHeader("User-Agent", "PostmanRuntime/7.13.0");
                request.AddHeader("Content-Type", "application/json");
                request.AddHeader("Authorization", "Bearer Ue3NWkomm5p9d9KleHssvMzfGCL33l3p");
                request.AddParameter("undefined", "{\n    \"app_id\": \"5e201bfe0715680006b9e82d\",\n    \"entry_id\": \"62f4e6fc82654a00088ae09b\",\n    \"limit\": 5,\n    \"filter\": {\n        \"rel\": \"and\", \n        \"cond\": [\n           \n            {\n                \"field\": \"_widget_1579163533839\",\n                \"type\": \"text\",\n                \"method\": \"eq\",\n                \"value\": \"" + SN + "\"\n            }\n        ]\n    }\n\n}", ParameterType.RequestBody);
                IRestResponse response = client.Execute(request);
                //var client = new RestClient("https://api.jiandaoyun.com/api/v4/app/5e201bfe0715680006b9e82d/entry/62625acbde6db4000ac9c49b/data");
                ////client.Timeout = -1;
                //var request = new RestRequest(Method.POST);
                //request.AddHeader("Authorization", "Bearer Ue3NWkomm5p9d9KleHssvMzfGCL33l3p");
                //request.AddHeader("Content-Type", "application/json");
                //request.AddHeader("Cookie", "_csrf=s%3A-MboxWaeasc5ivmBCkWcA3jK.GoRv26RtU6MwHLD4ob%2FHwdKCfOruFMzd58OoJDfKXA8");
                //request.AddParameter("application/json", "{\r\n    \"limit\": 1,\r\n    \"fields\": [\"_widget_1579163533820\",\"_widget_1651889643442\",\"_widget_1636015236938\",\"_widget_1589606291059\"],\r\n    \"filter\": {\r\n                \"cond\": [\r\n                            {\r\n                                \"field\": \"_widget_1579163533839\",\r\n                                \"method\": \"eq\",\r\n                                \"value\": \"" + SN + "\"\r\n                            }\r\n                        ]\r\n                }\r\n}", ParameterType.RequestBody);
                //IRestResponse response = client.Execute(request);
                //Console.WriteLine(response.Content);
                string factMessage = response.Content;
                string[] yunArray = factMessage.Split(new string[] { ",", ":" }, StringSplitOptions.RemoveEmptyEntries);
                char[] charsToTrim = { '"', ' ', '\'' };
                string PCBSNO = ""; string 原始PRTI = "";
                for (int i = 0; i < yunArray.Count(); i++)
                {
                    if (yunArray[i].Contains("_widget_1579163533820"))
                    {
                        PN = yunArray[i + 1].Trim(charsToTrim);
                        int index = PN.IndexOf("B");
                        if (index != -1)
                        { PN = PN.Substring(index, PN.Count() - index); }                      
                    }
                    if (yunArray[i].Contains("_widget_1636015238558"))
                    {
                        原始PRTI = yunArray[i + 1].Trim(charsToTrim);
                    }
                    if (yunArray[i].Contains("_widget_1650613044351"))
                    {
                        PCBSNO = yunArray[i + 1].Trim(charsToTrim);
                    }
                    if (yunArray[i].Contains("_widget_1651889644167"))
                    {
                        bengyuanSN = yunArray[i + 1].Trim(charsToTrim).Substring(0, 10);
                    }
                    if (yunArray[i].Contains("_widget_1650784572876"))
                    {
                        RLP = yunArray[i + 1].Trim(charsToTrim);
                    }
                    if (yunArray[i].Contains("_widget_1636015237204"))
                    {
                        PowerPN = yunArray[i + 1].Trim(charsToTrim);
                    }
                    
                }
                for (int i = 0; i < 原始PRTI.Length; i++)
                {
                    if (原始PRTI[i] >= '0' && 原始PRTI[i] <= '9')
                    {
                        PRTI = PRTI + 原始PRTI[i];
                    }
                }
                if (PRTI == null || PRTI.Length == 0)
                {
                    PRTI = "0";
                }
                for (int i = 0; i < PCBSNO.Length; i++)
                {
                    if (Public.Seekfirstchar("数字", PCBSNO))
                    {
                        PCBSN = PCBSNO.Substring(0, 3);
                        break;
                    }
                    else
                    {
                        PCBSNO = PCBSNO.Substring(1, PCBSNO.Length - 1);
                    }
                }

                //RLP= RLP.ToUpper();
                //int ID = RLP.IndexOf("MW");
                //RLP = RLP.Substring(0, ID);
                RLP = "260";
                if (Public.RLPCD.ContainsKey(RLP))
                {
                    RLP = Public.RLPCD[RLP].ToString();
                }

                return true;
            }
            catch (Exception ex)
            {
                 TraceMethodInfo(ex.Message+ex.StackTrace);
                PN = ""; PRTI = ""; RLP = ""; PCBSN = ""; bengyuanSN = ""; PowerPN="";
                return false;
            }

        }
        public static void GETIP()
        {
            string SN = "FMNG017359";
             SN = "BFL-CW2000T-A000005";
            //PN = ""; PRTI = ""; RLP = ""; PCBSN = ""; bengyuanSN = "";

            var client = new RestClient("https://api.jiandaoyun.com/api/v4/app/5e201bfe0715680006b9e82d/entry/62625acbde6db4000ac9c49b/data");
            //client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Authorization", "Bearer Ue3NWkomm5p9d9KleHssvMzfGCL33l3p");
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Cookie", "_csrf=s%3A-MboxWaeasc5ivmBCkWcA3jK.GoRv26RtU6MwHLD4ob%2FHwdKCfOruFMzd58OoJDfKXA8");
            request.AddParameter("application/json", "{\r\n    \"limit\": 1,\r\n    \"fields\": [\"_widget_1579163533820\",\"_widget_1651889643442\",\"_widget_1636015236938\",\"_widget_1589606291059\"],\r\n    \"filter\": {\r\n                \"cond\": [\r\n                            {\r\n                                \"field\": \"_widget_1579163533839\",\r\n                                \"method\": \"eq\",\r\n                                \"value\": \"" + SN + "\"\r\n                            }\r\n                        ]\r\n                }\r\n}", ParameterType.RequestBody);
            IRestResponse response = client.Execute(request);
            //Console.WriteLine(response.Content);
            string factMessage = response.Content;
            string[] yunArray = factMessage.Split(new string[] { ",", ":" }, StringSplitOptions.RemoveEmptyEntries);
            char[] charsToTrim = { '"', ' ', '\'' };
            string PN = yunArray[2].Trim(charsToTrim);
            string PRTI = yunArray[56].Trim(charsToTrim).Substring(0, 2);
            string PCBSNO = yunArray[58].Trim(charsToTrim);
            string PCBSN;
            for (int i = 0; i < PCBSNO.Length; i++)
            {
                if (Public.Seekfirstchar("数字", PCBSNO))
                {
                    PCBSN = PCBSNO.Substring(0, 3);
                    break;
                }
                else
                {
                    PCBSNO = PCBSNO.Substring(1, PCBSNO.Length - 1);
                }
            }
            string RLP = yunArray[85].Trim(charsToTrim);
            string bengyuanSN = yunArray[17].Trim(charsToTrim).Substring(0, 10);
            int ID = RLP.IndexOf("mW");
            RLP = RLP.Substring(0, ID);
            if (Public.RLPCD.ContainsKey(RLP))
            {
                RLP = Public.RLPCD[RLP].ToString();
            }
        }
    }
}
